﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using Esponce.Common;
using Esponce.QRCode;

namespace Esponce.Models
{
	public class TrackInfoViewModel : INotifyPropertyChanged
	{
		#region Constructor
		public TrackInfoViewModel()
		{
			if (Settings.IsDesignMode)
			{
				this.Id = 10;
				this.ApiKey = "9b02ec5da819ff14450c56583211eff3";
				this.Info = new QRCodeTrackListItem()
				{
					Id = 99,
					Name = "Lorem Ipsum",
					Comment = "comment...",
					Address = "Nowhere",
					Longitude = 10,
					Latitude = 10,
					Count = 100,
					NewCount = 10,
					Created = DateTime.UtcNow,
					OriginalUrl = "http://www.example.com",
					TrackUrl = "http://goo.by/example",
					//QRCodeImageUrl = "",
				};
				this.Items = new List<QRCodeTrackInfoItem>()
				{
					new QRCodeTrackInfoItem()
					{
						Id = 99,
						DeviceManufacturerName = "Samsung",
						DeviceModelName = "Omnia 7",
						Created = DateTime.UtcNow
					},
					new QRCodeTrackInfoItem()
					{
						Id = 100,
						DeviceManufacturerName = "Apple",
						DeviceModelName = "iPad",
						Created = DateTime.UtcNow
					},
					new QRCodeTrackInfoItem()
					{
						Id = 101,
						DeviceManufacturerName = "Samsung",
						DeviceModelName = "Galaxy S",
						Created = DateTime.UtcNow
					}
				};
			}
		}
		#endregion

		#region Public properties
		public int Id
		{
			get
			{
				return this.id;
			}
			set
			{
				if (this.id != value)
				{
					this.id = value;
					OnPropertyChanged("Id");
				}
			}
		}
		private int id;

		public string ApiKey
		{
			get
			{
				return this.apiKey;
			}
			set
			{
				if (this.apiKey != value)
				{
					this.apiKey = value;
					OnPropertyChanged("ApiKey");
				}
			}
		}
		private string apiKey;

		public QRCodeTrackListItem Info
		{
			get
			{
				return this.info;
			}
			set
			{
				if (this.info != value)
				{
					this.info = value;
					OnPropertyChanged("Info");
				}
			}
		}
		private QRCodeTrackListItem info;

		public List<QRCodeTrackInfoItem> Items
		{
			get
			{
				return this.items;
			}
			set
			{
				if (this.items != value)
				{
					this.items = value;
					OnPropertyChanged("Items");
				}
			}
		}
		private List<QRCodeTrackInfoItem> items;
		#endregion

		#region Public methods
		public void GetTrackInfo(int id)
		{
			try
			{
				//Make an API request
				var client = new QRCodeClient();
				client.Format = App.ViewModel.SelectedDataFormat;
				var response = client.GetTrackInfo(id, this.ApiKey);
				this.Info = response.Info;
				this.Items = response.Items;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
			}
		}
		#endregion

		#region INotifyPropertyChanged Members
		public event PropertyChangedEventHandler PropertyChanged;
		protected virtual void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
		#endregion
	}
}
